import { Router } from 'express';
import { PaymentController } from '../controllers/PaymentController';

const router: Router = Router();
const paymentController = new PaymentController();

/**
 * @swagger
 * components:
 *   schemas:
 *     PaymentIntent:
 *       type: object
 *       properties:
 *         paymentIntentId:
 *           type: string
 *           description: 支付意图ID
 *         clientSecret:
 *           type: string
 *           description: 客户端密钥
 *         amount:
 *           type: integer
 *           description: 金额（分）
 *         currency:
 *           type: string
 *           description: 货币代码
 *         status:
 *           type: string
 *           description: 支付状态
 *         allowRedirects:
 *           type: boolean
 *           description: 是否允许重定向支付方式
 *     CreatePaymentIntentInput:
 *       type: object
 *       required:
 *         - amount
 *         - userId
 *       properties:
 *         amount:
 *           type: integer
 *           minimum: 1
 *           description: 支付金额（分）
 *         currency:
 *           type: string
 *           default: usd
 *           description: 货币代码
 *         description:
 *           type: string
 *           description: 支付描述
 *         metadata:
 *           type: object
 *           description: 自定义元数据
 *         userId:
 *           type: string
 *           description: 用户ID
 *         allowRedirects:
 *           type: boolean
 *           default: false
 *           description: 是否允许重定向支付方式（如银行转账等）。设为false仅支持信用卡等直接支付方式，设为true支持所有Stripe支付方式
 *     Payment:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *           description: 支付ID
 *         amount:
 *           type: integer
 *           description: 金额（分）
 *         currency:
 *           type: string
 *           description: 货币代码
 *         status:
 *           type: string
 *           description: 支付状态
 *         description:
 *           type: string
 *           description: 支付描述
 *         created:
 *           type: integer
 *           description: 创建时间戳
 *     Subscription:
 *       type: object
 *       properties:
 *         subscriptionId:
 *           type: string
 *           description: 订阅ID
 *         status:
 *           type: string
 *           description: 订阅状态
 *         currentPeriodStart:
 *           type: integer
 *           description: 当前周期开始时间
 *         currentPeriodEnd:
 *           type: integer
 *           description: 当前周期结束时间
 *         trialStart:
 *           type: integer
 *           description: 试用开始时间
 *         trialEnd:
 *           type: integer
 *           description: 试用结束时间
 *         cancelAtPeriodEnd:
 *           type: boolean
 *           description: 是否在周期结束时取消
 *     CreateSubscriptionInput:
 *       type: object
 *       required:
 *         - userId
 *         - priceId
 *       properties:
 *         userId:
 *           type: string
 *           description: 用户ID
 *         priceId:
 *           type: string
 *           description: 价格ID
 *         trialPeriodDays:
 *           type: integer
 *           description: 试用期天数
 *         metadata:
 *           type: object
 *           description: 自定义元数据
 *     StripeConfig:
 *       type: object
 *       properties:
 *         publishableKey:
 *           type: string
 *           description: Stripe公钥
 */

/**
 * @swagger
 * /api/payments/config:
 *   get:
 *     tags: [Payments]
 *     summary: 获取Stripe配置
 *     description: 获取Stripe公钥，用于前端集成Stripe.js
 *     responses:
 *       200:
 *         description: 成功获取配置
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/StripeConfig'
 *                 message:
 *                   type: string
 *                   example: 获取公钥成功
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: Stripe公钥未配置
 */
router.get('/config', paymentController.getPublishableKey);

/**
 * @swagger
 * /api/payments/payment-intent:
 *   post:
 *     tags: [Payments]
 *     summary: 创建支付意图
 *     description: 创建一个新的支付意图，返回客户端密钥用于前端确认支付
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/CreatePaymentIntentInput'
 *     responses:
 *       201:
 *         description: 支付意图创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/PaymentIntent'
 *                 message:
 *                   type: string
 *                   example: 支付意图创建成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: 用户ID是必需的
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.post('/payment-intent', paymentController.createPaymentIntent);

/**
 * @swagger
 * /api/payments/payment-intent/{paymentIntentId}/confirm:
 *   post:
 *     tags: [Payments]
 *     summary: 确认支付
 *     description: 确认支付意图，完成支付流程
 *     parameters:
 *       - in: path
 *         name: paymentIntentId
 *         required: true
 *         schema:
 *           type: string
 *         description: 支付意图ID
 *     responses:
 *       200:
 *         description: 支付确认成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     paymentIntentId:
 *                       type: string
 *                     status:
 *                       type: string
 *                     amount:
 *                       type: integer
 *                     currency:
 *                       type: string
 *                 message:
 *                   type: string
 *                   example: 支付确认成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.post('/payment-intent/:paymentIntentId/confirm', paymentController.confirmPayment);

/**
 * @swagger
 * /api/payments/payment-intent/{paymentIntentId}/confirm-with-method:
 *   post:
 *     tags: [Payments]
 *     summary: 使用支付方式确认支付
 *     description: 在后端创建支付方式并确认支付（仅用于测试，生产环境不推荐）
 *     parameters:
 *       - in: path
 *         name: paymentIntentId
 *         required: true
 *         schema:
 *           type: string
 *         description: 支付意图ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - paymentMethod
 *             properties:
 *               paymentMethod:
 *                 type: object
 *                 required:
 *                   - type
 *                   - card
 *                 properties:
 *                   type:
 *                     type: string
 *                     enum: [card]
 *                     description: 支付方式类型
 *                   card:
 *                     type: object
 *                     required:
 *                       - number
 *                       - exp_month
 *                       - exp_year
 *                       - cvc
 *                     properties:
 *                       number:
 *                         type: string
 *                         description: 信用卡号码
 *                         example: "4242424242424242"
 *                       exp_month:
 *                         type: integer
 *                         description: 过期月份
 *                         example: 12
 *                       exp_year:
 *                         type: integer
 *                         description: 过期年份
 *                         example: 2025
 *                       cvc:
 *                         type: string
 *                         description: CVC码
 *                         example: "123"
 *                   billing_details:
 *                     type: object
 *                     properties:
 *                       name:
 *                         type: string
 *                         description: 持卡人姓名
 *                         example: "Test User"
 *                       email:
 *                         type: string
 *                         description: 邮箱地址
 *                         example: "test@example.com"
 *     responses:
 *       200:
 *         description: 支付确认成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     paymentIntentId:
 *                       type: string
 *                     status:
 *                       type: string
 *                     amount:
 *                       type: integer
 *                     currency:
 *                       type: string
 *                 message:
 *                   type: string
 *                   example: 支付确认成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.post('/payment-intent/:paymentIntentId/confirm-with-method', paymentController.confirmPaymentWithMethod);

/**
 * @swagger
 * /api/payments/payment-intent/{paymentIntentId}:
 *   get:
 *     tags: [Payments]
 *     summary: 获取支付意图详情
 *     description: 获取指定支付意图的详细信息
 *     parameters:
 *       - in: path
 *         name: paymentIntentId
 *         required: true
 *         schema:
 *           type: string
 *         description: 支付意图ID
 *     responses:
 *       200:
 *         description: 成功获取支付意图
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     paymentIntentId:
 *                       type: string
 *                     status:
 *                       type: string
 *                     amount:
 *                       type: integer
 *                     currency:
 *                       type: string
 *                     description:
 *                       type: string
 *                     created:
 *                       type: integer
 *                 message:
 *                   type: string
 *                   example: 获取支付意图成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/payment-intent/:paymentIntentId', paymentController.getPaymentIntent);

/**
 * @swagger
 * /api/payments/payment-intent/{paymentIntentId}/cancel:
 *   post:
 *     tags: [Payments]
 *     summary: 取消支付
 *     description: 取消指定的支付意图
 *     parameters:
 *       - in: path
 *         name: paymentIntentId
 *         required: true
 *         schema:
 *           type: string
 *         description: 支付意图ID
 *     responses:
 *       200:
 *         description: 支付取消成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     paymentIntentId:
 *                       type: string
 *                     status:
 *                       type: string
 *                       example: canceled
 *                 message:
 *                   type: string
 *                   example: 支付取消成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.post('/payment-intent/:paymentIntentId/cancel', paymentController.cancelPayment);

/**
 * @swagger
 * /api/payments/users/{userId}/payments:
 *   get:
 *     tags: [Payments]
 *     summary: 获取用户支付历史
 *     description: 获取指定用户的支付历史记录
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: offset
 *         schema:
 *           type: integer
 *           default: 0
 *         description: 偏移量
 *     responses:
 *       200:
 *         description: 成功获取支付历史
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Payment'
 *                 message:
 *                   type: string
 *                   example: 获取支付历史成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/users/:userId/payments', paymentController.getUserPayments);

/**
 * @swagger
 * /api/payments/subscriptions:
 *   post:
 *     tags: [Payments]
 *     summary: 创建订阅
 *     description: 为用户创建新的订阅计划
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/CreateSubscriptionInput'
 *     responses:
 *       201:
 *         description: 订阅创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   allOf:
 *                     - $ref: '#/components/schemas/Subscription'
 *                     - type: object
 *                       properties:
 *                         clientSecret:
 *                           type: string
 *                           description: 客户端密钥（如果需要确认支付）
 *                 message:
 *                   type: string
 *                   example: 订阅创建成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: 用户ID和价格ID是必需的
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.post('/subscriptions', paymentController.createSubscription);

/**
 * @swagger
 * /api/payments/subscriptions/{subscriptionId}/cancel:
 *   post:
 *     tags: [Payments]
 *     summary: 取消订阅
 *     description: 取消指定的订阅，可以选择立即取消或在当前周期结束时取消
 *     parameters:
 *       - in: path
 *         name: subscriptionId
 *         required: true
 *         schema:
 *           type: string
 *         description: 订阅ID
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               cancelAtPeriodEnd:
 *                 type: boolean
 *                 default: true
 *                 description: 是否在当前周期结束时取消
 *     responses:
 *       200:
 *         description: 订阅取消成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     subscriptionId:
 *                       type: string
 *                     status:
 *                       type: string
 *                     cancelAtPeriodEnd:
 *                       type: boolean
 *                     canceledAt:
 *                       type: integer
 *                 message:
 *                   type: string
 *                   example: 订阅取消成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.post('/subscriptions/:subscriptionId/cancel', paymentController.cancelSubscription);

/**
 * @swagger
 * /api/payments/users/{userId}/subscriptions:
 *   get:
 *     tags: [Payments]
 *     summary: 获取用户订阅列表
 *     description: 获取指定用户的所有订阅
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 成功获取订阅列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Subscription'
 *                 message:
 *                   type: string
 *                   example: 获取订阅列表成功
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/users/:userId/subscriptions', paymentController.getUserSubscriptions);

/**
 * @swagger
 * /api/payments/webhook:
 *   post:
 *     tags: [Payments]
 *     summary: Stripe Webhook处理
 *     description: 处理来自Stripe的Webhook事件，需要原始请求体和签名验证
 *     security: []
 *     parameters:
 *       - in: header
 *         name: stripe-signature
 *         required: true
 *         schema:
 *           type: string
 *         description: Stripe签名
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             description: Stripe事件对象
 *     responses:
 *       200:
 *         description: Webhook处理成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 received:
 *                   type: boolean
 *                   example: true
 *       400:
 *         description: 签名验证失败
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: Webhook签名验证失败
 *                 error:
 *                   type: string
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: Webhook密钥未配置
 */
router.post('/webhook', paymentController.handleWebhook);

export default router;